import request from '@utils/request';
import * as actionTypes from './actionType'
import { message } from 'antd';
import moment from 'moment';

//获取所有告警信息
export const getAllWarningRequest = (page) => {
    return (dispatch) => {
        request('POST', '/api/server/web/rest/warning/getAllWarning', {
            cmd: 'getAllWarning',
            type: 'request',
            request: {
                ...page
            }
        }).then(obj => {
            let res = obj.response;
            if (res.res) {
                let totalCount=res.totalCount
                let result =res.message;
                let data=modifyingtimestamps(result);
                dispatch((getAllPlanReducer(data,totalCount)))
            } else {
                message.error('获取告警失败' + res.exception, 1);
            }
        }).catch(err => console.log(err));
    }
}

const getAllPlanReducer = (warninglist,totalCount) => ({
    type: actionTypes.GETALLWARNING,
    warninglist,
    totalCount
})

//根据所选条件查询告警信息
export const getWarningByASTTMD = (information) => {
    return (dispatch) => {

        request('POST', '/api/server/web/rest/warning/getWarningByASTTMD', {
            cmd: 'getWarningByASTTMD',
            type: 'request',
            request: {
                ...information
            }
        }).then(obj => {
            let res = obj.response;
            if (res.res) {
                let totalCount=res.totalCount
                let result = res.message;
                let data = modifyingtimestamps(result);
                message.success('查找成功', 1);
                dispatch((getAllPlanReducer(data,totalCount)))
            } else {
                message.error('查找失败' + res.exception, 1);
            }
        }).catch(err => console.log(err));
    }
}

//将时间戳改成正常时间
const modifyingtimestamps =(data)=>{
    let array =data

    for(var i=0;i<data.length;i++){
        array[i].distributeTime=moment(data[i].distributeTime).format('YYYY-MM-DD HH:mm')
        array[i].feedbackTime=moment(data[i].feedbackTime).format('YYYY-MM-DD HH:mm')
        array[i].warningTime=moment(data[i].warningTime).format('YYYY-MM-DD HH:mm')
    }
    return array
}